package com.cy.air.postXml.model
{
	import com.cy.air.postXml.ApplicationFacade;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;

	public class XmlRequestProxy extends Proxy implements IProxy
	{
		public static const NAME:String="xmlRequestProxy";
		
		public var requesturl:String;
		
		public var requestText:String;
		
		public var responseText:String;
		
		public function XmlRequestProxy()
		{
			super(NAME);
		}
		
		public function sendXml():void{
			if(requesturl!=null){
				
				var xml:XML=new XML(requestText);
				
				var hs:HTTPService=new HTTPService();
				hs.contentType="application/xml";
				hs.url=requesturl;
				hs.method="post";
				hs.addEventListener(ResultEvent.RESULT,doresponse);
				hs.addEventListener(FaultEvent.FAULT,dofault);
				hs.send(xml);
				
				sendNotification(ApplicationFacade.UPDATE_REQUEST_URL,requesturl);
			}
		}
		
		private function dofault(e:FaultEvent):void{
			responseText=" StatusCode:"+e.statusCode+"\n Message: \n " + e.message;
			
			sendNotification(ApplicationFacade.UPDATE_RESPONSE_TEXT,responseText);
		}
		
		private function doresponse(e:ResultEvent):void{
			responseText=e.result.toString();
			sendNotification(ApplicationFacade.UPDATE_RESPONSE_TEXT,responseText);
		}
		
	}
}